﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace FYPHP
{
    public partial class AdminEditStaffProfile : System.Web.UI.Page
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);
    

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    SqlCommand cmdUser;
                    SqlDataReader dtrUser;
                    conn.Open();

                  

                    cmdUser = new SqlCommand("Select * From Users where u_id=@UID ", conn);
                    cmdUser.Parameters.AddWithValue("@UID", Request.QueryString["id"].ToString());


                    dtrUser = cmdUser.ExecuteReader();
                    dtrUser.Read();


                    String userName = dtrUser["username"].ToString();
                    String password = dtrUser["password"].ToString();
                    String IC = dtrUser["u_ic"].ToString();
                    String age = dtrUser["u_age"].ToString();
                    String gender = dtrUser["u_gender"].ToString();

                    String address = dtrUser["u_address"].ToString();
                    string addressBeforeSplit = dtrUser["u_address"].ToString();
                    string[] lines = addressBeforeSplit.Split('|');


                    String phone = dtrUser["u_phone"].ToString();
                    String email = dtrUser["u_email"].ToString();
                    String rewardpoint = dtrUser["u_points"].ToString();
                    String UserStatus = dtrUser["u_status"].ToString();
                    String UserImage = dtrUser["u_picture"].ToString();


                    lblName.Text = userName;
                    txtIC.Text = IC;
                    txtAge.Text = age;
                    rblGender.SelectedValue = gender;
                    txtAdd1.Text = lines[0].ToString();
                    txtAdd2.Text = lines[1].ToString();
                    txtPostCode.Text = lines[2].ToString();
                    txtCity.Text = lines[3].ToString();
                    txtState.Text = lines[4].ToString();
                    txtCountry.Text = lines[5].ToString();
                    txtPhone.Text = phone;
                    txtEmail.Text = email;
                    lblRewardPoints.Text = rewardpoint;
                    imgName.Text = UserImage;

                    dtrUser.Close();
                    conn.Close();

                }
                catch (SqlException ex)
                {
                    Response.Write("Invalid at Edit Staff Profile - Page_Load");
                }
            }
        }

        public String return_userpicture()
        {
            SqlCommand cmdUser;
            SqlDataReader dtrUser;

            conn.Open();

            cmdUser = new SqlCommand("Select * From Users WHERE u_id=@UID", conn);
            cmdUser.Parameters.AddWithValue("@UID", Request.QueryString["id"].ToString());
            dtrUser = cmdUser.ExecuteReader();
            dtrUser.Read();
            String userPicture = dtrUser["u_picture"].ToString();


            dtrUser.Close();
            conn.Close();

            return userPicture;

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            string uploadFolder = Request.PhysicalApplicationPath + "/uploads/users/";

            if (FileUpload1.HasFile)
            {
                string now = String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now);
                string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                FileUpload1.SaveAs(uploadFolder + now + extension);
                UploadStatusLabel.Text = "File uploaded successfully as: ";
                imgName.Text = now + extension;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }
        }

        protected void btnUpdateProfile_Click(object sender, EventArgs e)
        {
            try
            {

                String IC = txtIC.Text;
                int Age = Convert.ToInt32(txtAge.Text);
                String Gender = rblGender.SelectedValue;
                String fullAddress = txtAdd1.Text + "|" + txtAdd2.Text + "|" + txtPostCode.Text + "|" + txtCity.Text + "|" + txtState.Text + "|" + txtCountry.Text;
                String Email = txtEmail.Text;
                String PhoneNo = txtPhone.Text;

                conn.Open();

                SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_ic=@Ic,u_age=@Age,u_gender=@Gender,u_address=@Address,u_email=@Email,u_phone=@Phone,u_picture=@Picture where u_id =@Id ", conn);
                

                cmdUpdate.Parameters.AddWithValue("@Ic", IC);
                cmdUpdate.Parameters.AddWithValue("@Age", Age);
                cmdUpdate.Parameters.AddWithValue("@Gender", Gender);
                cmdUpdate.Parameters.AddWithValue("@Address", fullAddress);
                cmdUpdate.Parameters.AddWithValue("@Phone", PhoneNo);
                cmdUpdate.Parameters.AddWithValue("@Email", Email);
                cmdUpdate.Parameters.AddWithValue("@Picture", imgName.Text);
                cmdUpdate.Parameters.AddWithValue("@Id", Request.QueryString["id"].ToString());

                int x = cmdUpdate.ExecuteNonQuery();

                

                conn.Close();
                MessageBox("Profile Update Successfully");
                Redirect("AdminControlPanel.aspx");
                

            }
            catch (SqlException ex)
            {

            }

        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }



    }
}